// pages/user/info.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');
let util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    custom: { country: 'Andorra', code: 'AD', area: "376" },
    reg: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this, country = util.country
    _this.setData({ country: country });
  },
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    let _this = this, user_token = wx.getStorageSync('user_token'), country = util.country;

    req.request(api.getUserEditableInfo, {data: {token: user_token}}, function(res) {
      res = res.data
      let custom = res;
      
      custom.country = !custom.country ? 'Andorra' : custom.country
      custom.code = !custom.code ? 'AD' : custom.code
      custom.area = !custom.area ? '376' : custom.area
      custom.firstName = custom.firstname
      custom.lastName = custom.lastname
      custom.fullName = custom.fullname

      delete(custom.firstname)
      delete(custom.fullname)
      delete(custom.lastname);

      for(let i in country) {
        if(country[i].area === custom.area) {
          _this.setData({ reg: country[i].reg ? country[i].reg : null });
        }
      }

      _this.setData({custom: custom})
      wx.hideLoading();
    })
  },
  changeCountry(e) {
    let id = e.currentTarget.id, country = util.country, area = e.currentTarget.dataset.area, _this = this, custom = _this.data.custom, name = e.currentTarget.dataset.name;

    custom.country = name
    custom.code = id
    custom.area = area

    for (let i in country) {
      if (country[i].area === area) {
        _this.setData({ reg: country[i].reg ? country[i].reg : null });
      }
    }

    _this.setData({ check_wrapper: '', check_content: '', country: country, custom: custom, inputed_country: '' })
    setTimeout(function () {
      _this.setData({ check_show: '' })
    }, 300);
  },

  //用户填写信息
  insertInfo(e) {
    let value = e.detail.value, _this = this, custom = _this.data.custom, name = e.currentTarget.dataset.name
    custom[name] = value
    _this.setData({ custom: custom })
  },
  search(e) {
    let _this = this, value = e.detail.value, country = util.country, result = []
    if (value != '') {
      for (let i in country) {
        if (country[i].name.toLowerCase().search(value.toLowerCase()) > -1 || country[i].code.toLowerCase().search(value.toLowerCase()) > -1) {
          result.push(country[i])
        }
      }
      _this.setData({ country: result, inputed_country: value });
    } else {
      _this.setData({ country: country, inputed_country: value });
    }
  },
  hideBox(e) {
    let _this = this, name = e.currentTarget.dataset.name;
    if (name == 'check') {
      _this.setData({ box_wrapper: '', box_content: '' })
      setTimeout(function () {
        _this.setData({ box_show: '' })
        _this.prevStep();
      }, 310);
    } else if (name == 'select') {
      _this.setData({ check_wrapper: '', check_content: '' })
      setTimeout(function () {
        _this.setData({ check_show: '' })
      }, 300);
    }
  },
  showSelect(e) {
    let _this = this, name = e.currentTarget.dataset.name;
    _this.setData({ check_show: 'select-show', option_name: name})
    setTimeout(function () {
      _this.setData({ check_wrapper: 'select-wrapper', check_content: 'select-content-show' })
    }, 20);
  },
  save() {
    let _this = this, custom = _this.data.custom, msg = '', flag = false, token = wx.getStorageSync('user_token'), reg = _this.data.reg;

    msg = !custom.fullName ? '请填写您的全名' : 
      !custom.firstName ? '请填写您的英文名' :
        !custom.lastName ? '请填写您的英文姓' :
          !custom.email ? '请填写您的电子邮箱' :
            !(/^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test(custom.email)) ? '请填写正确的电子邮箱' :
              // !custom.id_card ? '请填写您的身份证号码' :
                // !(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(custom.id_card)) ? '请填写正确的身份证号码' :
                  !custom.phone ? '请填写您的手机号码' : 
                    (reg !== null && !reg.test(`+${custom.area}${custom.phone}`)) ? '请填写正确的手机号码' : flag = true;

    if (!flag) {
      wx.showToast({
        title: msg,
        icon: 'none'
      })
      return;
    }

    custom.token = token;
    wx.showLoading({
      title: '加载中',
    })

    req.request(api.saveUserInfo, {data: custom}, function(res) {
      res = res.data;
      wx.hideLoading();
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })

      if(res.code == 1) {
        console.info(custom)
        wx.setStorageSync('custom', custom);
      }

      setTimeout(function() {
        wx.navigateBack({
          delta: 1,
        })
      }, 3000);
    })
  }
})